package com.lottery.ruok.lottrey.adapter;

import android.content.Context;
import android.graphics.Color;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.activity.LiuhecaiPrizeActivity;
import com.lottery.ruok.lottrey.retrofitModule.response.LiuhecaiPrizeResponse;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import utils.Utils;

/**
 * Created by ASUS on 2017/11/13.
 */

public class LHCPrizeNumberAdapter extends BaseAdapter {
    private ArrayList<LiuhecaiPrizeResponse> liuhecaiPrizeResponses;
    private Context mContext;
    private LiuhecaiPrizeActivity liuhecaiPrizeActivity;

    public LHCPrizeNumberAdapter(ArrayList<LiuhecaiPrizeResponse> liuhecaiPrizeResponses, Context mContext, LiuhecaiPrizeActivity liuhecaiPrizeActivity) {
        this.liuhecaiPrizeResponses = liuhecaiPrizeResponses;
        this.mContext = mContext;
        this.liuhecaiPrizeActivity = liuhecaiPrizeActivity;
    }

    @Override
    public int getCount() {
        if(liuhecaiPrizeResponses == null){
            return 0;
        }
        return liuhecaiPrizeResponses.size();
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view != null) {
            holder = (ViewHolder) view.getTag();
        } else {
            view = LayoutInflater.from(mContext).inflate(R.layout.lhc_prize_number_item, viewGroup, false);
            holder = new ViewHolder(view);
            view.setTag(holder);
        }

        holder.issue.setText(liuhecaiPrizeResponses.get(position).getIssue_no());
        holder.time.setText(liuhecaiPrizeResponses.get(position).getDate());
        holder.container.removeAllViews();
        if (liuhecaiPrizeResponses.get(position).prizeList.size() > 0 && !liuhecaiPrizeResponses.get(position).prizeList.get(0).equals("")) {
            for (int i = 0; i < liuhecaiPrizeResponses.get(position).prizeList.size(); i++) {
                View areaCircle = LayoutInflater.from(mContext).inflate(R.layout.framework_prize_ball, null);
                TextView number = areaCircle.findViewById(R.id.number);
                number.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
                number.setText(liuhecaiPrizeResponses.get(position).prizeList.get(i));

                LinearLayout linearLayout = new LinearLayout(mContext);
                linearLayout.setOrientation(LinearLayout.VERTICAL);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(Utils.dip2px(mContext, 27), Utils.dip2px(mContext, 27));
                layoutParams.bottomMargin = Utils.dip2px(mContext, 1);
                areaCircle.setLayoutParams(layoutParams);
                linearLayout.addView(areaCircle);

                TextView textView = new TextView(mContext);
                if (liuhecaiPrizeResponses.get(position).colors.size() > 0 && !liuhecaiPrizeResponses.get(position).colors.get(0).equals("")) {
                    switch (liuhecaiPrizeResponses.get(position).colors.get(i)) {
                        case "blue":
                            if (liuhecaiPrizeResponses.get(position).chooses.get(i) && liuhecaiPrizeResponses.get(position).choosesCondition.get(i)) {
                                areaCircle.setBackgroundResource(R.mipmap.blue_big);
                                textView.setTextColor(Color.parseColor("#3f3f3f"));
                                number.setTextColor(Color.parseColor("#000000"));
                            } else {
                                areaCircle.setBackgroundResource(R.mipmap.blue_big_zhe);
                                textView.setTextColor(Color.parseColor("#e6e6e6"));
                                number.setTextColor(Color.parseColor("#e6e6e6"));
                            }
                            break;
                        case "red":
                            if (liuhecaiPrizeResponses.get(position).chooses.get(i) && liuhecaiPrizeResponses.get(position).choosesCondition.get(i)) {
                                areaCircle.setBackgroundResource(R.mipmap.red_big);
                                textView.setTextColor(Color.parseColor("#3f3f3f"));
                                number.setTextColor(Color.parseColor("#000000"));

                            } else {
                                areaCircle.setBackgroundResource(R.mipmap.red_big_zhe);
                                textView.setTextColor(Color.parseColor("#e6e6e6"));
                                number.setTextColor(Color.parseColor("#e6e6e6"));
                            }
                            break;
                        case "green":
                            if (liuhecaiPrizeResponses.get(position).chooses.get(i) && liuhecaiPrizeResponses.get(position).choosesCondition.get(i)) {
                                areaCircle.setBackgroundResource(R.mipmap.green_big);
                                textView.setTextColor(Color.parseColor("#3f3f3f"));
                                number.setTextColor(Color.parseColor("#000000"));
                            } else {
                                areaCircle.setBackgroundResource(R.mipmap.green_big_zhe);
                                textView.setTextColor(Color.parseColor("#e6e6e6"));
                                number.setTextColor(Color.parseColor("#e6e6e6"));
                            }
                            break;
                    }

                    switch (liuhecaiPrizeActivity.currentAttribute) {
                        case "生肖":
                            textView.setText(liuhecaiPrizeResponses.get(position).sxs.get(i));
                            break;
                        case "五行":
                            textView.setText(liuhecaiPrizeResponses.get(position).wxs.get(i));
                            break;
                        case "家禽野兽":
                            textView.setText(liuhecaiPrizeResponses.get(position).qinshous.get(i));
                            break;
                        case "男女生肖":
                            textView.setText(liuhecaiPrizeResponses.get(position).nannvs.get(i));
                            break;
                        case "天地生肖":
                            textView.setText(liuhecaiPrizeResponses.get(position).tiandis.get(i));
                            break;
                        case "四季生肖":
                            textView.setText(liuhecaiPrizeResponses.get(position).sijis.get(i));
                            break;
                        case "琴棋书画":
                            textView.setText(liuhecaiPrizeResponses.get(position).qinqishuhuas.get(i));
                            break;
                        case "三色生肖":
                            textView.setText(liuhecaiPrizeResponses.get(position).sebos.get(i));
                            break;
                    }
                    textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
                    textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                    textView.setEms(1);
                }

                linearLayout.addView(textView);
                LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                layoutParams2.rightMargin = Utils.dip2px(mContext, 9);
                linearLayout.setLayoutParams(layoutParams2);
                linearLayout.setGravity(Gravity.CENTER_HORIZONTAL);
                holder.container.addView(linearLayout);

                if (i == liuhecaiPrizeResponses.get(position).prizeList.size() - 2) {//倒数第二个
                    ImageView add = new ImageView(mContext);
                    add.setImageResource(R.mipmap.jiahui);
                    LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(Utils.dip2px(mContext, 10), Utils.dip2px(mContext, 10));
                    layoutParams1.rightMargin = Utils.dip2px(mContext, 9);
                    layoutParams1.topMargin = Utils.dip2px(mContext, 10);
                    add.setLayoutParams(layoutParams1);
                    holder.container.addView(add);
                }
            }
        }

        return view;
    }

    static class ViewHolder {
        @BindView(R.id.time)
        TextView time;
        @BindView(R.id.issue)
        TextView issue;
        @BindView(R.id.container)
        LinearLayout container;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}